﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;

namespace Tesla.Graphics {
    /// <summary>
    /// Provides a general way to bind vertex buffers to the graphics pipeline.
    /// </summary>
    public struct VertexBufferBinding {
        private VertexBuffer _buffer;
        private int _vertexOffset;
        private int _instanceFrequency;

        /// <summary>
        /// Gets the vertex buffer bound.
        /// </summary>
        public VertexBuffer VertexBuffer {
            get {
                return _buffer;
            }
        }

        /// <summary>
        /// Gets or sets the index from the beginning of the buffer and the vertex data to use.
        /// </summary>
        public int VertexOffset {
            get {
                return _vertexOffset;
            }
        }

        /// <summary>
        /// Gets or sets the instance frequency, which specifies how many times to draw the instance before
        /// stepping one unit forward.
        /// </summary>
        /// <value>Value of one means draw one instance, value of two, two instances and so on. Leave this
        /// value zero if not using instancing.</value>
        public int InstanceFrequency {
            get {
                return _instanceFrequency;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="VertexBufferBinding"/>.
        /// </summary>
        /// <param name="buffer">The vertex buffer.</param>
        public VertexBufferBinding(VertexBuffer buffer) {
            if(buffer == null) {
                throw new ArgumentNullException("buffer", "Vertex buffer cannot be null.");
            }
            _buffer = buffer;
            _vertexOffset = 0;
            _instanceFrequency = 0;
        }

        /// <summary>
        /// Creates a new instance of <see cref="VertexBufferBinding"/>.
        /// </summary>
        /// <param name="buffer">The vertex buffer.</param>
        /// <param name="vertexOffset">The vertex offset.</param>
        public VertexBufferBinding(VertexBuffer buffer, int vertexOffset) {
            if(buffer == null) {
                throw new ArgumentNullException("buffer", "Vertex buffer cannot be null.");
            }

            if(vertexOffset < 0 || vertexOffset >= buffer.VertexCount) {
                throw new ArgumentOutOfRangeException("vertexOffset", "Vertex offset must be greater than zero or less than or equal to the vertex count.");
            }

            _buffer = buffer;
            _vertexOffset = vertexOffset;
            _instanceFrequency = 0;
        }

        /// <summary>
        /// Creates a new instance of <see cref="VertexBufferBinding"/>.
        /// </summary>
        /// <param name="buffer">The vertex buffer.</param>
        /// <param name="vertexOffset">The vertex offset.</param>
        /// <param name="instanceFrequency">The instance frequency.</param>
        public VertexBufferBinding(VertexBuffer buffer, int vertexOffset, int instanceFrequency) {
            if(buffer == null) {
                throw new ArgumentNullException("buffer", "Vertex buffer cannot be null.");
            }

            if(vertexOffset < 0 || vertexOffset >= buffer.VertexCount) {
                throw new ArgumentOutOfRangeException("vertexOffset", "Vertex offset must be greater than zero or less than or equal to the vertex count.");
            }

            if(instanceFrequency < 0) {
                throw new ArgumentOutOfRangeException("instanceFrequency", "Instance frequency must be zero or greater.");
            }

            _buffer = buffer;
            _vertexOffset = vertexOffset;
            _instanceFrequency = instanceFrequency;
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="Tesla.Graphics.VertexBuffer"/> to <see cref="Tesla.Graphics.VertexBufferBinding"/>.
        /// </summary>
        /// <param name="buffer">The buffer.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator VertexBufferBinding(VertexBuffer buffer) {
            return new VertexBufferBinding(buffer);
        }
    }
}
